package com.springboot.jeesite.modules.sys.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.springboot.jeesite.modules.sys.entity.User;
import com.springboot.jeesite.modules.sys.entity.UserOnline;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.apache.shiro.subject.SimplePrincipalCollection;
import org.apache.shiro.subject.support.DefaultSubjectContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * 待完善
 *
 * @author bootdo
 */
@Service
public class SessionService {
    private final SessionDAO sessionDAO;

    @Autowired
    public SessionService(SessionDAO sessionDAO) {
        this.sessionDAO = sessionDAO;
    }

    public List<UserOnline> list(String username) {
        List<UserOnline> list = new ArrayList<>();
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        for (Session session : sessions) {
            UserOnline userOnline = new UserOnline();
            if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
                continue;
            } else {
                SimplePrincipalCollection principalCollection = (SimplePrincipalCollection) session
                        .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
                //User user = (User) principalCollection.getPrimaryPrincipal();
                String loginName = String.valueOf(BeanUtil.beanToMap(principalCollection.getPrimaryPrincipal()).get("loginName"));
                if (StrUtil.isNotEmpty(username) && !loginName.equals(username)) {
                    continue;
                }
                userOnline.setUsername(loginName);
            }
            userOnline.setId((String) session.getId());
            userOnline.setHost(session.getHost());
            userOnline.setStartTimestamp(session.getStartTimestamp());
            userOnline.setLastAccessTime(session.getLastAccessTime());
            userOnline.setTimeout(session.getTimeout());
            userOnline.setOs(String.valueOf(session.getAttribute("os")));
            userOnline.setBrowser(String.valueOf(session.getAttribute("browser")));
            userOnline.setStatus("在线");
            list.add(userOnline);
        }
        return list;
    }

    public List<User> listOnlineUser() {
        List<User> list = new ArrayList<>();
        User user;
        Collection<Session> sessions = sessionDAO.getActiveSessions();
        for (Session session : sessions) {
            SimplePrincipalCollection principalCollection = new SimplePrincipalCollection();
            if (session.getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY) == null) {
                continue;
            } else {
                principalCollection = (SimplePrincipalCollection) session
                        .getAttribute(DefaultSubjectContext.PRINCIPALS_SESSION_KEY);
                user = (User) principalCollection.getPrimaryPrincipal();
                list.add(user);
            }
        }
        return list;
    }

    public Collection<Session> sessionList() {
        return sessionDAO.getActiveSessions();
    }

    public boolean forceLogout(String sessionId) {
        Session session = sessionDAO.readSession(sessionId);
        session.setTimeout(0);
        return true;
    }
}
